package com.hstypay.demo.example.handler;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.hstypay.cmq.annotation.CmqQueueHandler;
import com.hstypay.cmq.queue.CmqQueueConsumer;
import com.hstypay.cmq.queue.ICmqQueueHandler;
import com.hstypay.cmq.support.AcknowledgeMode;
import com.hstypay.demo.dto.HsUserDTO;
import com.hstypay.demo.dto.HsUserMessageDTO;
import com.hstypay.demo.facade.HsUserMessageService;

@Service
@CmqQueueHandler(queue="hs-user-queue", ackMode=AcknowledgeMode.AUTO)
public class HsUserQueueHandler implements ICmqQueueHandler<HsUserDTO> {
	private static final Logger log = LoggerFactory.getLogger(HsUserQueueHandler.class);
	
	@Resource
	private HsUserMessageService hsUserMessageService;
	
	@Override
	public void onMessage(HsUserDTO msgBody, CmqQueueConsumer consumer) {
		// TODO Auto-generated method stub
		String jsonStr = JSON.toJSONStringWithDateFormat(msgBody, "yyyy-MM-dd", SerializerFeature.WriteDateUseDateFormat);
		log.info("接收CMQ消息处理，队列名称：{}，消息：{}", consumer.getQueueName(), jsonStr);
		List<HsUserMessageDTO> messageList = hsUserMessageService.queryByUserId(msgBody.getId());
		log.info("{}-消息列表：{}", msgBody.getName(), JSON.toJSONStringWithDateFormat(messageList, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteDateUseDateFormat));
	}

}
